//
//  WishListViewController.swift
//  CJXSwiftStoryboard
//
//  Created by CJX on 2016/10/22.
//  Copyright © 2016年 caijianxi. All rights reserved.
//

import UIKit
import SVProgressHUD
import SwiftyJSON

class WishListViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,WishListTableViewCellDelegate {

    var myTableView = UITableView(frame: CGRect(x: 0, y: 64, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-64), style: .plain)
    
    var dataArray = NSMutableArray()
    let WISHLISTCELL = "wishListCell"
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.creatNavVIew()
        self.getData()
        self.creatTableView()
        
    }
    
    
}


extension WishListViewController{
    
    fileprivate func creatNavVIew(){
        
        self.automaticallyAdjustsScrollViewInsets = false

        self.view.backgroundColor = UIColor.white
        self.navigationController?.navigationBar.barTintColor = UIColor(red: 94/255.0, green: 200/255.0, blue: 252/255.0, alpha: 1)
        self.title = "WISHLIST"
        
        let leftBtn = UIButton(type: .custom)
        leftBtn.setImage(UIImage(named: "nav_back"), for: UIControlState())
        leftBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        leftBtn.addTarget(self, action: #selector(leftBtnClick), for: .touchUpInside)
        let item = UIBarButtonItem(customView: leftBtn)
        self.navigationItem.leftBarButtonItem = item
    }
    
    func leftBtnClick(){
        self.dismiss(animated: true) { 
            
        }
    }
}

extension WishListViewController{
    fileprivate func getData(){
        
        if UserSession.shareInstance.isLogin == true {
            let parma = ["main_page":"my_wishlist","zenid":UserSession.shareInstance.cookie!]
            
            NetworkTool.shareInstance.request(.get, urlString: APIBASE, parmas: parma as [String : NSObject], finishedBlock: { (result, error) in
                if result != nil{
//                    let json = JSON(result!)
//                    print(json)
                    
                    let arr = (result as? NSDictionary)!["data"]as?[[String:NSObject]]
                    for dic in arr!{
                        let model = WishListModel(dic: dic as NSDictionary)
                        self.dataArray.add(model)
                    }
                    self.myTableView.reloadData()
                }else{
                    SVProgressHUD.showError(withStatus: "get orderData error:" + String(describing: error))
                    print("get orderData error:" + String(describing: error))
                }
            })
            
        }
        
    }
    
}
extension WishListViewController{
    fileprivate func creatTableView(){
        myTableView.delegate = self
        myTableView.dataSource = self
        self.view.addSubview(myTableView)
        let nib = UINib(nibName: "WishListTableViewCell", bundle: nil)
        myTableView.register(nib, forCellReuseIdentifier: WISHLISTCELL)
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: WISHLISTCELL)
        let model = self.dataArray[indexPath.row]as?WishListModel
        (cell as? WishListTableViewCell)?.model = model
        cell?.selectionStyle = .none
        (cell as? WishListTableViewCell)?.delegate = self
        return cell!
        
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return SCREEN_HEIGHT * 0.2
    }
    
    
    func viewButtonDelegateWithID(_ id: String, imageStr: String) {
        let commodityVC = CommodityViewController()
        commodityVC.idStr = id
        commodityVC.productImgView = imageStr
        self.navigationController?.pushViewController(commodityVC, animated: true)
    }
    func deleteButtonDelegateWithID(_ id: String) {
        let urlStr = APIBASE + "main_page=wishlist_actions"
        let parma = ["act":"remove","products_id":id,"zenid":UserSession.shareInstance.cookie!]
        NetworkTool.shareInstance.request(.post, urlString: urlStr, parmas: parma as [String : NSObject]) { (result, error) in
            if result != nil{
                SVProgressHUD.showSuccess(withStatus: "Delete the success!")
                self.dataArray.removeAllObjects()
                self.getData()
                self.myTableView.reloadData()
            }else{
                SVProgressHUD.showError(withStatus: "get orderData error:" + String(describing: error))
                print("get orderData error:" + String(describing: error))
            }
        }
        
        
    }
}










    
